const db = uniCloud.database()
const photoDB = db.collection("secondPhotoList")
module.exports = {
	_before: function() { // 通用预处理器

	},
	async addPhotoRecord(event, context) {
		try {
			const {
				openid,
				path,
				userId,
				isAsync
			} = event
			let addInfo = await photoDB.add({
				openid,
				path,
				state: 'send',
				isAsync,
				showCount: 0,
				userId,
				createTime: new Date().getTime()
			})
			return {
				code: 200,
				dataImgId: addInfo.id,
				msg: '添加成功'
			}
		} catch (e) {
			//TODO handle the exception
			return {
				code: 500,
				msg: e
			}
		}
	},
	// 根据openid获取所有照片记录
	async getPhotoRecord(event, contest) {
		try {
			let res = await photoDB.where({
				openid: event.openid
			}).orderBy('createTime', 'desc').get()
			return {
				code: 200,
				data: res
			}
		} catch (e) {
			return {
				code: 500,
				data: e
			}
		}
	},
	// 获取照片
	async useIdGetPhotoInfo(event, contest) {
		try {
			const {
				id,
			} = event
			let {
				data
			} = await photoDB.where({
				_id: id
			}).get()
			// const updateData = {
			// 	...data[0],
			// 	showCount: data[0].showCount + 1
			// }
			// if (!isUser) {
			// 	delete updateData._id
			// 	let updateRes = await photoDB.doc(id)
			// 		.update(updateData)
			// }

			return {
				code: 200,
				msg: '查看照片成功',
				data: data[0]
			}
		} catch (e) {
			return {
				code: 500,
				data: e
			}
		}

	},
	// 记录操作
	async usePhotoInfoAndHandle(event) {
		try {

			let {
				data
			} = await photoDB.where({
				_id: event.imgId
			}).get()
			const updateData = {
				...data[0],
				showCount: data[0].showCount + 1
			}
			delete updateData._id
			let updateRes = await photoDB.doc(event.imgId)
				.update(updateData)

			const handleRecord = db.collection("viewPhotoList")
			let addInfo = await handleRecord.add({
				...event
			})
			console.log('event', event);

			return {
				code: 200,
				msg: '操作成功',
			}
		} catch (e) {
			return {
				code: 500,
				data: e
			}
		}
	},
	// 获取一个图片的操作记录
	async getHandleRecord(event) {
		try {

			let limit = 100; // 每次查询的数量
			let offset = 0; // 偏移量	
			const whereInfo = {
				imgId: event.imgId
			}

			let resultData = await db.collection('viewPhotoList').where(whereInfo).orderBy('createTime', 'desc')
				.skip(offset).limit(limit).get()
			return {
				code: 200,
				msg: '操作成功',
				data: resultData.data
			}


			// let resultCount = await db.collection('viewPhotoList').where(whereInfo).count(); // 总数量		

			// do {
			// 	let resultData = await db.collection('viewPhotoList').where(whereInfo).skip(offset).limit(limit)
			// 		.get();
			// 	console.log('resultData', resultData);
			// 	userList = userList.concat(resultData.data); // 将查询到的数据添加到列表中	 
			// 	offset += limit; // 更新偏移量
			// } while (offset < resultCount.total && offset<500);
			// return {
			// 	code: 200,
			// 	msg: '操作成功',
			// 	data: userList.sort((a, b) => b.createTime - a.createTime)
			// }
		} catch (e) {
			return {
				code: 500,
				data: e
			}
		}
	},
	// 获取发送到照片广场的数据
	async getAsyncPhoto(event) {
		try {

			let limit = 100; // 每次查询的数量
			let offset = 0; // 偏移量	
			const whereInfo = {
				isAsync: true,
				showCount: db.command.gt(50),
			}
			let resultCount = await photoDB.where(whereInfo).count(); // 总数量		
			let userList = []
			do {
				let resultData = await photoDB.where(whereInfo).skip(offset).limit(limit)
					.get();
				console.log('resultData', resultData);
				userList = userList.concat(resultData.data); // 将查询到的数据添加到列表中	 
				offset += limit; // 更新偏移量
			} while (offset < resultCount.total);
			return {
				code: 200,
				msg: '操作成功',
				data: userList.sort((a, b) => a.showCount - b.showCount)
			}
		} catch (e) {
			return {
				code: 500,
				data: e
			}
		}
	},
	// 删除照片
	async delImgRecord(event) {
		try {
			const whereInfo = {
				_id: event.imgId
			}
			photoDB
			// 使用_id作为条件进行删除，注意_id应该是一个字符串
			const result = await photoDB.where(whereInfo).remove({
				$id: event.imgId
			});
			console.log('删除数据结果:', result);
			return {
				code: 200,
				data: result
			};
		} catch (error) {
			console.error('删除数据时发生错误:', e);
			return error
		}
	},
	/**
	 * @name 分割线
	 * ----------------------------------
	 * ----------------------------------
	 * ----------------------------------
	 * ----------------------------------
	 * ----------------------------------
	 * ----------------------------------
	 * ----------------------------------
	 */
	// async handleImgAll() { 
	// 	let resultCount = await photoDB.count(); // 总数量		
	// 	let limit = 100; // 每次查询的数量
	// 	let offset = 0; // 偏移量	
	// 	let userList = []
	// 	// let resultCount = await photoDB.get(); // 总数量
	// 	do {
	// 		let resultData = await photoDB.skip(offset).limit(limit)
	// 			.get();
	// 		console.log('resultData', resultData);
	// 		userList = userList.concat(resultData.data); // 将查询到的数据添加到列表中	 
	// 		offset += limit; // 更新偏移量
	// 	} while (offset < resultCount.total);
	// 	 // 遍历所有文档
	// 	 for (const doc of userList) {
	// 		 // 对每个文档执行更新操作，为newField字段设置默认值
	// 		 await photoDB.doc(doc._id).update({
	// 			isAsync: true
	// 		});
	// 	 }
	// },

}